#!/usr/bin/env python3
# from my_dbg import *
from List import List
from Dir_tree import *

from pythonic_c_release import *

# single_file = Dir_tree()/"3_src"/"single.c"

# s_list = list(single_file)

def file_filter(file, switch):
    f_list = list(file)
    file << switch(f_list)

def file_filter_create(src_file, dst_file, switch):
    dst_file << switch(list(src_file))
######################################################今天设计一个完全的空行格式。
def list_filter(l, swith):
    r_list = []
    for line in l:
        new_line = swith(line)
        if new_line != None:
            r_list.append(new_line)
    return r_list

def multi_list_filter(l, switch, n):
    #if n > len(l): return l
    r_list = []
    pre_index = 0
    for i in range(n, len(l)):
        r = switch(l[i-n:i])
        if r != None:
            r_list += l[pre_index:i-n] + r
            pre_index = i
    r_list += l[pre_index:]
    return r_list

#between function add empty line.
def add_empty_line(s_list):
    r = []
    for line in s_list:
        r.append(line)
        if line == "}":
            r.append("")
            r.append("")
            r.append("")
            
    return r
def code_pass(s_list):
    s_list = list_filter(s_list, lambda s: s[:-1] if s.strip()==";" else s)
    #s_list = list_filter(s_list, lambda s: None if s.strip()=="" else s)
    #s_list = list_filter(s_list, lambda s: None if s.strip()=="DS_PREPARE;" else s)
    s_list = list_filter(s_list, lambda s: None if s.strip()=="_Pass;" else s)
    
    s_list = list_filter(s_list, lambda s: ";".join(s.split(";;")) if ";;" in s else s)
    s_list = list_filter(s_list, lambda s: s[:-1] if len(s)>=2 and s[-2:]=="};" else s)
    #check_e = 'DS_EXCEPTON_CHECK("");'
    #s_list = list_filter(s_list, lambda s: "".join(s.split(check_e)) if check_e in s else s)
    #check_e = "DS_ERROR = "
    #s_list = list_filter(s_list, lambda s: "".join(s.split(check_e)) if check_e in s else s)

    #multi
    #s_list = multi_list_filter(s_list, lambda s3: [] if s3[0].strip()=="else" and s3[1].strip()=="{" and s3[2].strip()=="};" else None, 3)
    #s_list = multi_list_filter(s_list, lambda s2: [] if s2[0].strip()=="return NULL;" and s2[1].strip()=="return NULL;" else None, 2)
    
    #multi
    for i in range(3):
        s_list = multi_list_filter(s_list, lambda s3: [""] if s3[0].strip()=="" and s3[1].strip()=="" else None, 2)
    s_list = multi_list_filter(s_list, lambda s2: ["}"] if s2[0].strip()=="" and s2[1] =="}" else None, 2)
    #s_list = multi_list_filter(s_list, lambda s3: ["",""] if s3[0].strip()=="" and s3[1].strip()=="" else None, 2)
    #s_list = multi_list_filter(s_list, lambda s2: [""] if s2[0].strip()=="" and s2[1].strip()=="" else None, 2)

    #ds_prepare = "DS_PREPARE;"
    #s_list = list_filter(s_list, lambda s: "".join(s.split(check_e)) if check_e in s else s)
    
    #s_list = list_filter(s_list, lambda s: None if s.strip()==";" else s)
    
    # between function add empty line.
    s_list = add_empty_line(s_list)
    return s_list

def wash_c(src_dir, dst_dir, name):
    src_file = src_dir/(name + ".c")
    dst_file = dst_dir/(name + ".c")
    file_filter_create(src_file, dst_file, code_pass)


if __name__ == "__main__":
    import sys 
    src_name = sys.argv[1]
    dst_name = sys.argv[2]

    src_file = Dir_tree()/src_name
    dst_file = Dir_tree()/dst_name

    file_filter_create(src_file, dst_file, code_pass)
